package com.hubei.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubei.car.entity.*;
import com.hubei.car.mapper.CarThirdUserMapper;
import com.hubei.car.mapper.MsInquiryMapper;
import com.hubei.car.mapper.MsProductMapper;
import com.hubei.car.service.MsProductService;
import com.hubei.car.swaggerParam.MsInquiryAddParam;
import com.hubei.car.swaggerParam.MsProductQueryParam;
import com.hubei.car.swaggerParam.MsProductTypeQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MsProductServiceImpl implements MsProductService {

    @Autowired
    private MsProductMapper msProductMapper; 
    @Autowired
    private CarThirdUserMapper carThirdUserMapper;
    @Autowired
    private MsInquiryMapper msInquiryMapper;

    @Override
    public Result queryMsProductTypeTree() {
        return Result.ok(msProductMapper.queryMsProductTypeTree());
    }

    @Override
    public Result queryMsProductPageXml(MsProductQueryParam msProductQueryParam) {
        //分页
        Page<MsProduct> page = new Page<>(msProductQueryParam.getCurrent(), msProductQueryParam.getSize());
        IPage<MsProduct> msProductIPage = msProductMapper.queryMsProductPageXml(page, msProductQueryParam);
        if (msProductIPage == null) {
            return Result.ok(new PageResult(msProductQueryParam.getCurrent(), msProductQueryParam.getSize(), 0L, null));
        }
        return Result.ok(new PageResult(msProductQueryParam.getCurrent(), msProductQueryParam.getSize(), msProductIPage.getTotal(), msProductIPage.getRecords()));
    }

    @Override
    public Result queryMsProductTypePage(MsProductTypeQueryParam msProductTypeQueryParam) {
        //分页
        Page<MsProductType> page = new Page<>(msProductTypeQueryParam.getCurrent(), msProductTypeQueryParam.getSize());
        IPage<MsProductType> msProductTypeIPage = msProductMapper.queryMsProductTypePage(page, msProductTypeQueryParam);
        if (msProductTypeIPage == null) {
            return Result.ok(new PageResult(msProductTypeQueryParam.getCurrent(), msProductTypeQueryParam.getSize(), 0L, null));
        }
        return Result.ok(new PageResult(msProductTypeQueryParam.getCurrent(), msProductTypeQueryParam.getSize(), msProductTypeIPage.getTotal(), msProductTypeIPage.getRecords()));
    }

    @Transactional
    @Override
    public Result setMyFavoriteCar(Long productId, CarThirdUser carThirdUser) {
        CarThirdUser carThirdUserDB = carThirdUserMapper.selectById(carThirdUser.getId());
        carThirdUserDB.setMsProductId(productId);
        carThirdUserMapper.updateById(carThirdUserDB);
        return Result.ok("设置成功");
    }

    @Transactional
    @Override
    public Result addMsInquiry(MsInquiryAddParam msInquiryAddParam, CarThirdUser carThirdUser) {
        MsInquiry msInquiry = new MsInquiry();
        msInquiry.setName(msInquiryAddParam.getName());
        msInquiry.setMobile(msInquiryAddParam.getMobile());
        msInquiry.setProductId(msInquiryAddParam.getProductid());
        msInquiry.setUserId(carThirdUser.getId());
        msInquiryMapper.insert(msInquiry);
        return Result.ok("操作成功");
    }

    @Override
    public Result queryRecommendImages() {
        //分页
        Page<MsProduct> page = new Page<>(1,9);
        QueryWrapper<MsProduct> querywrapper = new QueryWrapper<>();
        querywrapper.isNotNull("defaultimages");
        page.setDesc("isrecommend","image_index");
        IPage<MsProduct> msProductIPage = msProductMapper.selectPage(page, querywrapper);
        List<MsProduct> msProductList = msProductIPage.getRecords();
        if (null != msProductList) {
            List<Map> resultMapList = new ArrayList<>();
            for (MsProduct msProduct : msProductList) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", msProduct.getId());
                String images = msProduct.getDefaultimages();
                String[] imagesUrl = images.split(",");
                map.put("imagesUrl", imagesUrl[0]);
                resultMapList.add(map);
            }
            return Result.ok(resultMapList);
        }
        return Result.ok(null);
    }
}
